<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain

                   @click="handleDelete">删 除
        </el-button>

      </template>

    </avue-crud>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove} from "@/api/business/retailPrice";
import {mapGetters} from "vuex";
import {getOne, getMedicineSecurityNo, getMedicineNo} from "@/api/business/medicine";

export default {
  data() {
    return {
      viewState: true,
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        //labelWidth: 130,
        // emptyBtn: false,
        searchLabelWidth:100,
        columnBtn: false,
        labelWidth: 150,
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: false,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        column: [
          {
            label: "id",
            prop: "id",
            type: "input",
            display: false,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide: true,
          },
          {
            label: "药品ID",
            prop: "medicineId",
            type: "input",
            addDisplay: false,
            display: false,
            overHidden: true, //控制自动隐藏超出部分
            editDisplay: false,
            rules: [{
              required: true,
              message: "请输入药品ID",
              trigger: "blur"
            }],
          },

          {
            label: "药品名称",
            prop: "medicineName",
            type: "select",
            search: true,
            dicUrl: "/api/retailPrice/listMedicineName?medicineName={{key}}",
            filterable: true,
            remote: true,
            props: {
              label: "medicineName",
              desc: "desc",
              value: "medicineNo"
            },
            change: ({column, value}) => {
              // console.log("value:", value)
              // debugger
              if (value===null){return}
              if (this.viewState) {
                if (value) {
                  // console.log("value:", value)
                  getMedicineNo(value).then(response => {
                    const data = response.data.data;
                    if (Object.keys(data).length !== 0) {

                      this.form.medicineId = data.id;
                      this.form.medicineNo = data.medicineNo;
                      this.form.medicineSecurityNo = data.insuranceCode;
                      // this.endData=data;
                      //  console.log(this.endData)

                      this.form.specification = data.specification;
                      this.form.packages = data.packages;
                      this.form.produceCompanyName = data.produceCompanyName;
                      this.form.bidPrice = data.bidPrice;
                      this.form.shotType = data.shotType;
                      //console.log(this.endData)
                    }
                  })
                } else {
                  // this.form.medicineName = null;
                }
              }
              // this.viewState = true;
            },
            span: 24,
            width: 180,
            overHidden: true, //控制自动隐藏超出部分
            editDisabled: true,
            rules: [{
              required: true,
              message: "请输入药品名称",
              trigger: "blur"
            }],
          },
          {
            label: "药品流水号",
            prop: "medicineNo",
            type: "select",
            dicUrl: "/api/retailPrice/listMedicineNo?medicineNo={{key}}",
            props: {
              label: "medicineNo",
              desc: "desc",
              value: "medicineNo"
            },
            change: ({column, value}) => {
              //
              // console.log("value:", value)

                // debugger
                //console.log(this.viewState)
                if (value) {
                  // console.log("value:", value)
                  getMedicineNo(value).then(response => {
                    const data = response.data.data;

                    if (Object.keys(data).length !== 0) {
                      this.form.medicineId = data.id;
                      this.form.medicineName = data.medicineName;
                      this.form.medicineSecurityNo = data.insuranceCode;
                      this.form.specification = data.specification;
                      this.form.packages = data.packages;
                      this.form.produceCompanyName = data.produceCompanyName;
                      this.form.bidPrice = data.bidPrice;
                      this.form.shotType = data.shotType;
                    }

                  })
                } else {
                  // this.form.medicineName = null;
                }

              // this.viewState = true;
            },
            overHidden: true, //控制自动隐藏超出部分
            filterable: true,
            editDisabled: true,
            remote: true,
            search: true,
            rules: [{
              required: true,
              message: "请输入药品流水号",
              trigger: "blur"
            }],
          },
          {
            label: "药品医保编号",
            prop: "medicineSecurityNo",
            type: "select",
            dicUrl: "/api/retailPrice/listMedicineSecurityNo?medicineSecurityNo={{key}}",
            editDisabled: true,
            props: {
              label: "insuranceCode",
              desc: "desc",
              value: "insuranceCode"
            },
            change: ({column, value}) => {
              // console.log("value:", value)
              // console.log(this.viewState)
              if (this.viewState) {
                if (value) {
                  // console.log(value)
                  //console.log(12345, "=====------------------------")
                  getMedicineSecurityNo(value).then(response => {
                    const data = response.data.data;

                    if (Object.keys(data).length !== 0) {
                      this.form.medicineId = data.id;
                      this.form.medicineName = data.medicineName;
                      this.form.medicineNo = data.medicineNo;

                      this.form.specification = data.specification;
                      this.form.packages = data.packages;
                      this.form.produceCompanyName = data.produceCompanyName;
                      this.form.bidPrice = data.bidPrice;
                      this.form.shotType = data.shotType;
                    }

                  })
                } else {
                  //this.form.medicineName = null;
                }
              }
              // this.viewState = true;
            },
            overHidden: true, //控制自动隐藏超出部分
            filterable: true,
            remote: true,
            search: true,
            rules: [{
              required: false,
              message: "请输入药品医保编号",
              trigger: "blur"
            }],
          },
          {
            label: "上报药店ID",
            prop: "pharmId",
            type: "input",
            display: false,
            hide: true,
            editDisplay: false,
            addDisplay: false,
            overHidden: true, //控制自动隐藏超出部分
          },

          {
            label: "上报药店名称",
            prop: "pharmName",
            type: "input",
            display: false,
            overHidden: true, //控制自动隐藏超出部分
            addDisplay: false,
            editDisplay: false,
          },
          {
            label: "租户id",
            prop: "tenantId",
            type: "input",
            display: false,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide: true,
          },
          {
            label: "规格",
            prop: "specification",
            type: "input",
            // readonly:true,
            overHidden: true,
            addDisabled: true,
            detail: true,
            //border:true,
            //editDisplay: false,
            //viewDisplay: false,
            hide: true,
          },

          {
            label: "挂网价中标价",
            prop: "bidPrice",
            type: "input",
            overHidden: true,
            addDisabled: true,
            detail: true,
            //border:false,
           // editDisplay: false,
            //viewDisplay: false,
            hide: true,
          },
          {
            label: "剂型",
            prop: "shotType",
            type: "input",
            addDisabled: true,
            detail: true,
            //editDisplay: false,
            //viewDisplay: false,
            hide: true,
            overHidden: true,
          },
          {
            label: "包装",
            prop: "packages",
            type: "input",
            detail: true,
            addDisabled: true,
           // editDisplay: false,
            //viewDisplay: false,
            hide: true,
            overHidden: true,
          },
          {
            label: "生产企业名称",
            prop: "produceCompanyName",
            type: "input",
            detail: true,
            span: 24,
            addDisabled: true,
            overHidden: true,
           // editDisplay: false,
            //viewDisplay: false,
            hide: true,
          },
          {
            label: "零售价",
            prop: "price",
            type: "input",
            span: 12,
            rules: [{
              required: true,
              message: "请输入零售价(整数或小数)",
              trigger: "blur",
              pattern: /^[0-9]+(\.[0-9]+)?$/,
            }],
          },
        ],
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.retailPrice_add, true),
        viewBtn: this.vaildData(this.permission.retailPrice_view, true),
        delBtn: this.vaildData(this.permission.retailPrice_delete, true),
        editBtn: this.vaildData(this.permission.retailPrice_edit, true)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        console.log(error);
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        //console.log(type);
        // console.log(this.viewState);
        this.viewState = false;
        // console.log(this.viewState);
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      } else {
        (this.viewState = true)
      }

      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      //console.log(params)
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
